package com.cicadalane.androlate;

/*
 * Copyright (C) 2011 cicada.software@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


import java.util.ArrayList;

public class StringUtils {

	public static String join(ArrayList<String> list) {
		return join(list, ':');
	}

	public static String join(ArrayList<String> list, char delim) {
		StringBuffer buf = new StringBuffer();
		for (int i = 0; i < list.size(); i++) {
			if (i != 0)
				buf.append(delim);
			buf.append((String) list.get(i));
		}
		return buf.toString();
	}

	public static String join(ArrayList<String> list, String delim) {
		StringBuffer buf = new StringBuffer();
		for (int i = 0; i < list.size(); i++) {
			if (i != 0)
				buf.append(delim);
			buf.append((String) list.get(i));
		}
		return buf.toString();
	}
    
	public static ArrayList<String> split(String s) {
		return split(s, ':');
	}

	public static ArrayList<String> split(String s, char delim) {
		ArrayList<String> rv = new ArrayList<String>();
		int length = s.length();
		int cur = -1;
		int next;

		if (length == 0)
			return rv;

		while (true) {
			next = s.indexOf(delim, cur + 1);
			if (next == -1) {
				rv.add(s.substring(cur + 1));
				break;
			} else if (next == length - 1) {
				rv.add(s.substring(cur + 1, next));
				break;
			} else {
				rv.add(s.substring(cur + 1, next));
				cur = next;
			}
		}
		return rv;
	}
	
	// split a string by n number of characters
    public static String[] split(String s, int chunkSize) {
        int slen = s.length();
        if (slen <= 0)
        	return new String[0];
        
        
        int splitCount = slen/chunkSize;
        if (slen % chunkSize > 0)
        	splitCount += 1;

        // initialize the result array
        String[] result = new String[splitCount];

        for (int i = 0; i < splitCount; i++) {
            int startPos = i * chunkSize;
            int endPos = startPos + chunkSize;
            if (endPos > slen)
                endPos = slen;
            String substr = s.substring(startPos, endPos);
            result[i] = substr;
        }
        return result;
    }

	ArrayList<String> m_stack = new ArrayList<String>();

	public String pop() {
		int size = m_stack.size();
		if (size == 0) {
			return null;
		} else {
			String rv = m_stack.get(size - 1);
			m_stack.remove(size - 1);
			return rv;
		}
	}

	public void push(String str) {
		m_stack.add(str);
	}
}
